package com.dhh.sky.widget;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.dhh.sky.R;
import com.dhh.sky.activity.FileListActivity;
import com.dhh.sky.model.SkyDriveAlbum;
import com.dhh.sky.model.SkyDriveFolder;
import com.dhh.sky.model.SkyDriveObject;
import com.dhh.sky.model.SkyDrivePhoto;
import com.dhh.sky.util.FileManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.readystatesoftware.viewbadger.BadgeView;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class FileListAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private FileListActivity context;
	private List<SkyDriveObject> files;  
	private List<SkyDriveObject> items;
	private List<CheckBox> boxes;
	private FileManager fm;
	private String path;
	private DisplayImageOptions options;
	private ImageLoader imageLoader = ImageLoader.getInstance();

	public FileListAdapter(FileListActivity context,String path){
		this.context = context;
		this.path = path;
		fm = new FileManager(context);
		inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		files = new ArrayList<SkyDriveObject>();  
		items = new ArrayList<SkyDriveObject>();
		boxes = new ArrayList<CheckBox>();
		initImangeLoader();
	}
	
	public void loadFiles(){
		this.files.clear();
		this.items.clear();
		this.boxes.clear();
		this.files.addAll(fm.getFiles(path));
	}
	
	protected void initImangeLoader(){
		imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        options = new DisplayImageOptions.Builder()
                                .showStubImage(R.drawable.image_picker_placeholder)
                                //.showImageForEmptyUrl(R.drawable.image_for_empty_url)
                                .cacheInMemory()
                                .cacheOnDisc()
                                .build();
	}
	
	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public SkyDriveObject getItem(int position) {
		return files.get(position);
	}

	@Override
	public long getItemId(int position) {
		long id = -1;
		if(position>files.size()-1){
			id = Long.valueOf(files.get(position).getId());
		}
		return id;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = inflater.inflate(R.layout.file_list_item,parent,false);
		final SkyDriveObject file = this.getItem(position);
		CheckBox check = (CheckBox) view.findViewById(R.id.fli_check);
		check.setChecked(items.contains(file));
		ImageView icon = (ImageView) view.findViewById(R.id.fli_icon);
		ImageView downloaded = (ImageView) view.findViewById(R.id.fli_downloaded);
		TextView name = (TextView) view.findViewById(R.id.fli_name);
		TextView detail = (TextView) view.findViewById(R.id.fli_detail);
		
		if(file.isPhoto()){
			imageLoader.displayImage(((SkyDrivePhoto)file).getThumbnailSrc(), icon, options);
		}else{
			icon.setImageResource(file.getIcon());
		}
		
		if(file.getType().equals(SkyDriveFolder.TYPE)){
			SkyDriveFolder folder = (SkyDriveFolder) file;
			showChildCounts(icon,folder.getCount());
		}else if(file.getType().equals(SkyDriveAlbum.TYPE)){
			SkyDriveAlbum folder = (SkyDriveAlbum) file;
			showChildCounts(icon,folder.getCount());			
		}
		
		if(fm.isDownloaded(file)){
			downloaded.setVisibility(View.VISIBLE);
		}else{
			downloaded.setVisibility(View.INVISIBLE);
		}
		
		name.setText(file.getName());
		detail.setText(file.getDetail());
		
		check.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View view) {
				CheckBox check = (CheckBox) view;
				if(!check.isChecked()){			    	
			    	for(int i=0; i<items.size(); i++){
			    		SkyDriveObject sf = items.get(i);
			    		if(sf.getId().equals(file.getId())){
			    			items.remove(i);
			    		}
			    	}	
				}else{
					SkyDriveObject file = getItem(position);			    	
			    	items.add(file);	
				}	    	
		    	context.modeChanged();
			}
		});
		boxes.add(check);
		
		return view;
	}
	
	public void selectFile(View view,int position){
    	CheckBox check = (CheckBox) view.findViewById(R.id.fli_check);
    	SkyDriveObject file = getItem(position);
    	
    	for(int i=0; i<items.size(); i++){
    		SkyDriveObject sf = items.get(i);
    		if(sf.getId().equals(file.getId())){
    			items.remove(i);
    			check.setChecked(false);
    			context.modeChanged();
    			return;
    		}
    	}
    	    	
    	items.add(file);
		check.setChecked(true); 	
    	
    	context.modeChanged();
    }
	
	private void showChildCounts(View icon, int count){
		BadgeView badge = new BadgeView(context, icon);
	    badge.setText(String.valueOf(count));
	    badge.setBadgePosition(BadgeView.POSITION_BOTTOM_RIGHT);
	    badge.setTextColor(Color.parseColor("#ffffff"));
	    badge.setTextSize(18);
	    badge.setBackgroundColor(Color.parseColor("#00000000"));
	    badge.show();
	}
	
	public void unSelectFiles(){
		for(CheckBox box : boxes){
			if(box!=null)
				box.setChecked(false);
		}
		
		items.clear();
	}
	
	public boolean isSingleChecked(){
		return items.size()==1;
	}
	
	public Long getMode(){
		Long mode = FileListActionMode.MODE_NONE;
		
		if(items.size()==0){
			return FileListActionMode.MODE_NONE;
		}else{
			boolean containFolder = false;
			for(SkyDriveObject item : items){
				if(SkyDriveFolder.TYPE.equals(item.getType()) || SkyDriveAlbum.TYPE.equals(item.getType())){
					containFolder = true;
					break;
				}
			}
			
			if(containFolder){
				mode = FileListActionMode.MODE_ALL;
			}else{
				mode = FileListActionMode.MODE_FILE;
			}
		}
		
		return mode;
	}
	
	public void setPath(String path) {
		this.path = path;
	}
	
	public void setFiles(List<SkyDriveObject> files){
		this.files.clear();
		this.items.clear();
		this.boxes.clear();
		this.files.addAll(files);
	}
	
	public List<SkyDriveObject> getItemsSelected(){
		return items;
	}
	
	public void hideItemsSelected(){
		for(SkyDriveObject item : items){
			files.remove(item);
		}
		this.notifyDataSetChanged();
	}
}
